package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.entity.CommentEntity;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.changuserinfo.reportActivity;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


public class VP2_recyleview_adapter extends RecyclerView.Adapter<VP2_recyleview_adapter.ViewHolder>{
    private final Context context;
    int pl_numbers;
    List<CommentEntity> commentEntities=new ArrayList<>();//评论信息

    //构造方法
    public VP2_recyleview_adapter(Context context, int pl_numbers){
        this.context=context;
        this.pl_numbers=pl_numbers;
    }
    //获取评论信息
    public void setcomentlistData(List<CommentEntity> commentEntities){
        if (commentEntities!=null)
            this.commentEntities=commentEntities;
        notifyDataSetChanged();
    }
    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{

        //评论
        RoundedImageView pl_head;
        TextView pl_name;
        TextView pl_sendtime;//发送时间
        TextView pl_usersays;//视频描述
        TextView pl_likenum;//视频描述
        TextView pl_unlickenum;//视频描述

        ImageView pl_like;//点赞评论
        ImageView pl_unlike;//点踩评论
        ImageView pl_report;//举报评论

        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            pl_sendtime=view.findViewById(R.id.send_time);
            pl_usersays=view.findViewById(R.id.text_pl);
            pl_head=view.findViewById(R.id.user_head);
            pl_name=view.findViewById(R.id.user_id);
            pl_likenum=view.findViewById(R.id.pl_like_num);//视频描述
            pl_unlickenum=view.findViewById(R.id.pl_unlike_num);//视频描述
            pl_like=view.findViewById(R.id.thum_img);
            pl_unlike=view.findViewById(R.id.unthum_img);
            pl_report=view.findViewById(R.id.repot);
        }
    }
    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
        view= LayoutInflater.from(context).inflate(R.layout.list_pinlunlist,parent,false);
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //页面评论
        if (commentEntities.size()>0){
            Glide.with(context).load(Uri.parse(commentEntities.get(position).getHead_photo())).into(holder.pl_head);
            holder.pl_name.setText(commentEntities.get(position).getUser_name());
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
            holder.pl_sendtime.setText(format.format(commentEntities.get(position).getComment_time()));
            holder.pl_usersays.setText(commentEntities.get(position).getComment_message());
            holder.pl_likenum.setText(commentEntities.get(position).getLike_num());
            holder.pl_unlickenum.setText(commentEntities.get(position).getUnlike_num());
        }
        if (holder.pl_like!=null){
            holder.pl_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (userStaticMsg.userId.equals("-1")){
                        Toast.makeText(context,"请登录",Toast.LENGTH_SHORT).show();
                    }else if (holder.pl_like.isSelected()){
                        Toast.makeText(context,"已经赞过了",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        GetData.thumbOrUnthumbpl(context,commentEntities.get(position).getComment_id(),true);

                        holder.pl_like.setSelected(true);
                        int a=Integer.parseInt(holder.pl_likenum.getText().toString());
                        holder.pl_likenum.setText(String.valueOf(a+1));
                        holder.pl_unlike.setSelected(false);
                    }
                }
            });//收藏事件
        }
        if (holder.pl_unlike!=null){
            holder.pl_unlike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (userStaticMsg.userId.equals("-1")){
                        Toast.makeText(context,"请登录",Toast.LENGTH_SHORT).show();
                    }else if (holder.pl_unlike.isSelected()){
                        Toast.makeText(context,"已经踩过了",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        GetData.thumbOrUnthumbpl(context,commentEntities.get(position).getComment_id(),false);

                        holder.pl_like.setSelected(false);
                        holder.pl_unlike.setSelected(true);
                        int a=Integer.parseInt(holder.pl_unlickenum.getText().toString());
                        holder.pl_unlickenum.setText(String.valueOf(a+1));
                    }
                }
            });//收藏事件
        }

        if (holder.pl_report!=null){
            holder.pl_report.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (userStaticMsg.userId.equals("-1")) {
                        Toast.makeText(context, "请登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent=new Intent(context, reportActivity.class);
                        intent.putExtra("comment_id",commentEntities.get(position).getComment_id());
                        intent.putExtra("comment_message",commentEntities.get(position).getComment_message());
                        context.startActivity(intent);
                    }

                }
            });//收藏事件
        }
    }

    @Override
    public int getItemCount() {
        return commentEntities.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 0;
        }else {
            return 1;
        }
    }
}
